<%@ page import="org.gnomus.server.C" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.List" %>
<%@ page import="org.gnomus.server.Context" %>
<%
  Context ctx = Context.get();
  List<Map> lessons = (List<Map>) ctx.data.get("lessons");
%>
<div class="f-left" style="width:480px;">
  <h3>Lessons</h3>
  <div id="lessons" style="min-height:200px;">
    <%
      if (lessons == null || lessons.size() == 0) {
    %>
    <p>
      No lessons found.
    </p>
    <%
      } else {
    %>
    <table style="width:100%;">
      <%
        for (Map lesson : lessons) {
          String id = (String) lesson.get("id");
          String href = "/admin/edit_lesson?id=" + id;
          String subject = C.LESSON_TOPICS.get(lesson.get("subject"));
      %>
      <tr>
        <td style="width:75%;">
          <a href='<%= href %>'>
            <%= subject + " : " + lesson.get("title") %>
          </a>
        </td>
        <td>
          <a title="duplicate lesson" onclick="duplicateLesson('<%= id %>')">
            <img src="/images/copy.png" alt="duplicate" style="width:24px;">
          </a>
        </td>
        <td>
          <a title="delete lesson" onclick="deleteLesson('<%= id %>')">
            <img src="/images/cancel.png" alt="delete" style="width:16px;">
          </a>
        </td>
      </tr>
      <%
        }
      %>
    </table>
  <%
    }
  %>
  </div>
</div>
<div class="f-right" style="width:400px;">
  <a class="f-right" href="/admin/import_lesson">
    Import a lesson
  </a>
  <h3>Create lesson</h3>
  <div id="create" class="border-1" style="width:100%; padding: 8px;">
    <form method="post" action="/admin/create_lesson" onsubmit="return validate($(this));">
      <table>
        <tr>
          <td class="a-right">
            <label for="name">
              Name:
            </label>
          </td>
          <td colspan="2" style="width:100%;">
            <input name="title" type="text" validation="required" style="width:92%;"/>
          </td>
        </tr>
        <tr>
          <td class="a-right">
            <label for="subject">
              Subject:
            </label>
          </td>
          <td>
            <select name="subject">
              <%
                for (Map.Entry<String, String> me : C.LESSON_TOPICS.entrySet()) {
              %>
                  <option value="<%= me.getKey() %>"><%= me.getValue() %></option>
              <%
                }
              %>
            </select>
          </td>
        </tr>
        <tr>
          <td colspan="2" class="a-right">
            <div>
              <input type="submit" value="Create">
            </div>
          </td>
        </tr>
      </table>
    </form>
  </div>
</div>
<div class="clear"></div>
<script type="text/javascript">
  function deleteLesson(id) {
    if (confirm("Are you sure you want to delete this lesson?")) {
      $.ajax({
        url : "/admin/delete_object?kind=lesson&id=" + id,
        type : "POST",
        success : function() { window.location.reload(true); }
      });
    }
  }

  function duplicateLesson(id) {
    var lesson;
    for (var i = 0; i < lessons.length; i++) {
      if (lessons[i].id == id) {
        lesson = lessons[i];
        break;
      }
    }
    var title = prompt("Enter a new name for the duplicate lesson", lesson.title);
    if (title && title.length > 1) {
      $.ajax({
        url : "/lesson/duplicate?id=" + id + "&title=" + title,
        type : "POST",
        success : function() { window.location.reload(true); }
      });
    }
  }
</script>
